'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url admin/sendmsg/pub/batch 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		const db = uniCloud.database();
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			_
		} = util;
		let {
			uid,
			msgs,
			things
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		// 存appid的token
		let apptoken = []

		for (const item1 of msgs) {
			if (!apptoken.find(item2 => item2.appid == item1.appid)) {
				const odata = await db.collection("a_appid").where({
					appid: item1.appid,
				}).get();
				const appsecret = odata.data[0].appsecret;
				const url = "https://developer.toutiao.com/api/apps/v2/token";
				const tokendata = await uniCloud.httpclient.request(url, {
					method: "POST",
					data: {
						appid: item1.appid,
						secret: appsecret,
						grant_type: "client_credential",
					},
					contentType: "json", // 指定以application/json发送data内的数据
					dataType: "json", // 指定返回值为json格式，自动进行parse
				});
				const token = tokendata.data.data.access_token;
				console.log("token=", token);
				apptoken.push({
					appid: item1.appid,
					access_token: token
				});
				item1.access_token = token
			} else {
				const tokenItem = apptoken.find(item2 => item2.appid == item1.appid);
				item1.access_token = tokenItem.access_token;
			}
		}


		console.log("msgs=1111111111111111111", msgs);
		console.log("apptoken111111111111111111111", apptoken);
		let batchRunRes = await vk.pubfn.batchRun({
			// 主执行函数
			main: async (item, index) => {
				await vk.pubfn.sleep((Math.floor(Math.random() * (3 - 0)) + 0) * 100);

				//====判断消息类型，执行不同函数====
				if (item.msgType == "qdtx") {
					console.log("qdtxitem=", item);
					const openid = item.openid
					const appid = item.appid
					const muban_id = item.muban_id
					qdtx(item.access_token, openid, appid, muban_id)



				}
				if (item.msgType == "rbxj") {
					console.log("rbxjmsgitem=", item);
					const openid = item.openid
					const appid = item.appid
					const muban_id = item.muban_id
					rbxj(item.access_token, openid, appid, muban_id)

				}
				if (item.msgType == "jjgx") {
					console.log("jjgxitem=", item);
					const openid = item.openid
					const appid = item.appid
					const muban_id = item.muban_id
					jjgx(item.access_token, openid, appid, muban_id)

				}
				//====判断消息类型结束====

				//====更改状态为已发送====
				const updtate = await db.collection('a_dingyue').doc(item._id).update({
					is_send: true
				})
				//====更改状态结束====

				return {
					code: 0,
					index
				}
			},
			// 最大并发量，如果设置为1，则会按顺序执行
			concurrency: 100,
			// 数据源，这些数据会依次跑一遍main函数
			data: msgs
		});


		// 业务逻辑结束-----------------------------------------------------------
		return res;

		async function qdtx(access_token, openid, appid, muban_id) {
			console.log("签到提醒参数qdtx------------------------------------", access_token, openid, appid,
				muban_id,
				things);

			// 签到提醒
			const pushmsg = {
				"access_token": access_token,
				"app_id": appid,
				"tpl_id": muban_id,
				"open_id": openid,
				"data": {
					"签到名称": things.thing1,
					"可领福利": things.thing2,
					"温馨提示": things.thing3,
				},
				"page": "/pages/index/index"
			}
			tosend(pushmsg)
		}

		async function jjgx(access_token, openid, appid, muban_id) {
			console.log("剧集更新参数jjgx-------------------------------------", access_token, openid, appid,
				muban_id,
				things);
			//剧集更新
			const pushmsg = {
				"access_token": access_token,
				"app_id": appid,
				"tpl_id": muban_id,
				"open_id": openid,
				"data": {
					"剧集标题": things.thing1,
					"更新集数": things.thing2,
					"更新时间": things.thing3,
					"描述": things.thing4,
				},
				"page": "/pages/index/index"
			}
			tosend(pushmsg)
		}

		async function rbxj(access_token, openid, appid, muban_id) {
			console.log("热门新剧rbxj-----------------------------------------", access_token, openid, appid,
				muban_id, things);
			//热播新剧
			const pushmsg = {
				"access_token": access_token,
				"app_id": appid,
				"tpl_id": muban_id,
				"open_id": openid,
				"data": {
					"热播新剧": things.thing1,
					"上线时间": things.thing3,
					"描述": things.thing2,
				},
				"page": "/pages/index/index"
			}
			tosend(pushmsg)
		}

		async function tosend(pushmsg) {
			let sendurl =
				"https://developer.toutiao.com/api/apps/subscribe_notification/developer/v1/notify";
			const send = await uniCloud.httpclient.request(sendurl, {
				method: 'POST',
				data: JSON.stringify(pushmsg),
				contentType: 'json', // 指定以application/json发送data内的数据
				dataType: 'json' // 指定返回值为json格式，自动进行parse
			})
			console.log("send=--------------------", send);
		}
	}
}